package com.common.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * @author 大忽悠
 * @create 2022/10/19 19:59
 */
public class MybatisUtil {

    /**
     * 资源重复校验
     * @param iService
     * @param paramName 资源对应的列名
     */
    public static <T,V> void repeatCheck(IService<T> iService,String paramName,V paramVal,String exMessage){
        T one = iService.getOne(new QueryWrapper<T>().eq(paramName, paramVal));
        Assert.isNull(one,exMessage);
    }

    public static <T,V> void repeatCheck(IService<T> iService, Map<String,Object> params, String exMessage){
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        params.forEach(queryWrapper::eq);
        long count = iService.count(queryWrapper);
        Assert.isTrue(count==0,exMessage);
    }

    /**
     * 资源必须存在检查
     * @param iService
     * @param paramName
     * @param paramVal
     * @param exMessage
     * @param <T>
     * @param <V>
     */
    public static <T,V> void resourceMustExist(IService<T> iService,String paramName,V paramVal,String exMessage){
        T one = iService.getOne(new QueryWrapper<T>().eq(paramName, paramVal));
        Assert.notNull(one,exMessage);
    }
}
